package com.model.common.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.model.common.base.activity.BaseActivity;
import com.model.common.databinding.ActivityWebBinding;
import com.model.common.utils.always.StringUtils;

/**
 * Create by Carson on 2021/12/22.
 * WebActivity
 */
public class WebActivity extends BaseActivity<ActivityWebBinding> {

    private String title;
    private int showType;//0文本内容,1外链
    private String content;

    @Override
    protected void getBundleExtras(Bundle extras) {
        title = extras.getString("title");
        showType = extras.getInt("showType");
        content = extras.getString("content");
    }

    @Override
    protected ActivityWebBinding getViewBinding() {
        return ActivityWebBinding.inflate(getLayoutInflater());
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initViewsAndEvents() {
        setCustomTitle(title);
        if (showType == 0) {
            binding.webView.loadDataWithBaseURL(null, StringUtils.getHtmlData(content),
                    "text/html", "utf-8", null);
        } else if (showType == 1) {//外链
            binding.webView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                }

                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    Log.e("sslError", "sslError" + error);
                    //信任证书
                    handler.proceed();
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if (url.startsWith("http") || url.startsWith("https")) {
                        view.loadUrl(url);
                        //返回false表示此url默认由系统处理,url未加载完成，会继续往下走
                        return false;
                    } else {
                        //加载的url是自定的协议地址
                        try {
                            //调用手机浏览器打开url
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(view.getUrl()));
                            startActivity(intent);
                        } catch (Exception e) {
                            Log.e("自定义协议地址加载失败", "url=" + view.getUrl()
                                    + "exception:" + e.toString());
                        }
                        return true;
                    }
                }
            });
            WebSettings webSettings = binding.webView.getSettings();
            //设定支持viewport
            webSettings.setUseWideViewPort(true);
            //自适应屏幕
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setDisplayZoomControls(false);
            webSettings.setBlockNetworkImage(false);
            webSettings.setJavaScriptEnabled(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            //设定支持缩放
            webSettings.setSupportZoom(true);
            webSettings.setDomStorageEnabled(true);
            binding.webView.loadUrl(StringUtils.avoidNull(content));
            Log.e("web", "url-------" + content);
        }
    }
}
